<?php

namespace App\Admin\Form;

use App\Exceptions\ApiExceptions;
use App\Models\CompanyModel;
use App\Models\JobpositionModel;
use App\Models\RegistrationLogModel;
use App\Services\Registration\RegistrationService;
use App\Services\User\UserService;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;


class CompanyRechargeFrom extends Form implements LazyRenderable
{
    use LazyWidget;
 

      /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        $id = $this->payload['id'] ?? null;
        $cur_type = $input['cur_type'] ?? null;
        $operate_at = $input['operate_at'];
        if ($cur_type == RegistrationLogModel::CUR_TYPE_ADD)
        {
            //充值
            $num = $input['add_num'];
            $res = RegistrationService::recharge(
                companyId: $id,
                num: abs($num),
                operate_at: $operate_at,
                remark: "后台增加金额：$num 元"
            );
        }else{
            //减少
            $num = $input['cut_num'];
            $res = RegistrationService::changeRegistrationNum(
                companyId: $id,
                changeNum: -abs($num),
                operate_at: $operate_at,
                remark: "后台减少金额：$num 元"
            );
        }
        if (!$res){
        return $this->response()->error('操作失败')->refresh();
        }
        return $this->response()->success('操作成功')->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $id = $this->payload['id'] ?? null;
        $info = CompanyModel::query()->find($id);
        $this->html("<div style='padding-top: 10px'> {$info->registration_num}个</div>")->label('报名单余额');
        $this->datetime('operate_at', '变更日期')->required()->default(date('Y-m-d H:i:s'));
        $this->radio('cur_type','变更数量')->options(RegistrationLogModel::TYPE_LIST)->required()
            ->default(RegistrationLogModel::CUR_TYPE_ADD)
            ->when(RegistrationLogModel::CUR_TYPE_ADD, function ($form) {
                $form->number('add_num','')->min(1)->rules('required_if:cur_type,add|min:1',[
                    'required_if'=>'不能为空'
                ]);
              
            })
            ->when(RegistrationLogModel::CUR_TYPE_CUT, function ($form) {
                $form->number('cut_num','')->min(1)->rules('required_if:cur_type,cut|min:1',[
                    'required_if'=>'不能为空'
                ]);
              
            });


    }

      /**
     * Build a form here.
     */
    // public function form()
    // {
    //     $id = $this->payload['id'] ?? null;
    //     $info = CompanyModel::query()->find($id);
    //     $this->html("<div style='padding-top: 10px'> {$info->registration_num}个</div>")->label('报名单余额');
    //     $this->datetime('operate_at', '变更日期')->required()->default(date('Y-m-d H:i:s'));
    //     $this->radio('cur_type','变更数量')->options(RegistrationLogModel::TYPE_LIST)->required()
    //         ->default(RegistrationLogModel::CUR_TYPE_ADD)
    //         ->when(RegistrationLogModel::CUR_TYPE_ADD, function ($form) {
    //             $form->number('add_num','')->min(1)->rules('required_if:cur_type,add|min:1',[
    //                 'required_if'=>'不能为空'
    //             ]);
    //             // $form->text('amount', '充值金额')->rules('required_if:cur_type,add|min:1',[
    //             //     'required_if'=>'不能为空'
    //             // ]);
    //         })
    //         ->when(RegistrationLogModel::CUR_TYPE_CUT, function ($form) {
    //             $form->number('cut_num','')->min(1)->rules('required_if:cur_type,cut|min:1',[
    //                 'required_if'=>'不能为空'
    //             ]);
    //             // $form->text('remark', '变更原因')->rules('required_if:cur_type,cut|min:1',[
    //             //     'required_if'=>'不能为空'
    //             // ]);
    //         });


    // }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return [
            //'account'         => '',
            //'number' => '',
        ];
    }
}
